package com.fiberhome.boot.demo.primary.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fiberhome.boot.demo.primary.dao.PrimaryRepository;
import com.fiberhome.boot.demo.primary.domain.Demo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value="/primary")
@Api(value = "mysql数据源测试", description="mysql数据源测试", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class PrimaryController {

	@Autowired
	PrimaryRepository repository;
	
	@ApiOperation(value="保存用户", notes="保存用户")
	@RequestMapping(value="/saveUser",method=RequestMethod.POST)
	public String saveUsers(@RequestBody Demo user){
		repository.save(user);
		return "save sucusess";
	}
	
	@ApiOperation(value="根据id获取用户", notes="根据id获取用户")
	@RequestMapping(value="/userById",method=RequestMethod.GET)
	public Demo getUserById(long id){
		return repository.findById(id);
	}
	
	@ApiOperation(value="根据id更新用户", notes="根据id更新用户")
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String upDateUser(@RequestBody Demo user){
		int response = repository.modifyByIdAndName(user.getId(), user.getName());
		return Integer.toString(response);
	}
	
	@ApiOperation(value="根据名字更新用户", notes="根据名字更新用户")
	@RequestMapping(value="/userByName",method=RequestMethod.GET)
	public List<Demo> getUserByName(String name){
		return repository.findByName(name);
	}
	
	@ApiOperation(value="获取用户记录条数", notes="获取用户记录条数")
	@RequestMapping(value="/userCount",method=RequestMethod.GET)
	public long getCount(){
		return repository.count();
	}
	
	@ApiOperation(value="获取所有用户记录", notes="获取所有用户记录")
	@RequestMapping(value="/users",method=RequestMethod.GET)
	public Iterable<Demo> getAll(){
		return repository.findAll();
	}
	
}
